/*
 * File Name:               UIGroupEditor.cs
 *
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2016/11/23
 */

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(UIGroup))]
public class UIGroupEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var group = target as UIGroup;

        GUILayout.Box("UI依赖关系群组", GUILayout.ExpandWidth(true));

        EditorGUILayout.BeginHorizontal();
        GUILayout.Box("自身分组 ID ", GUILayout.Width(200));
        group.pGroup = EditorGUILayout.IntField(group.pGroup);
        EditorGUILayout.EndHorizontal();

        GUILayout.Box("互斥 打开时要关闭的窗口分组", GUILayout.ExpandWidth(true));

        EditorGUILayout.BeginHorizontal();
        GUILayout.Box("删除", GUILayout.Width(200));
        GUILayout.Box("分组 ID", GUILayout.ExpandWidth(true));
        EditorGUILayout.EndHorizontal();

        var datas = group.pMutexGroups;
        if (datas == null)
            datas = new int[0];
        for (int i = 0; i < datas.Length; i++)
        {
            var child = datas[i];

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("-", GUILayout.Width(200)))
            {
                group.pMutexGroups = arrRemove(datas, child);
                return;
            }

            datas[i] = EditorGUILayout.IntField(child);
            EditorGUILayout.EndHorizontal();
        }

        if (GUILayout.Button("添加互斥分组"))
        {
            group.pMutexGroups = arrAdd(datas, 0);
        }

        GUILayout.Box("依赖 关闭时要关闭的窗口分组", GUILayout.ExpandWidth(true));

        EditorGUILayout.BeginHorizontal();
        GUILayout.Box("删除", GUILayout.Width(100));
        GUILayout.Box("分组 ID", GUILayout.ExpandWidth(true));
        EditorGUILayout.EndHorizontal();

        datas = group.pDependGroups;
        if (datas == null)
            datas = new int[0];
        for (int i = 0; i < datas.Length; i++)
        {
            var child = datas[i];

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("-", GUILayout.Width(100)))
            {
                group.pDependGroups = arrRemove(datas, child);
                return;
            }

            datas[i] = EditorGUILayout.IntField(child);
            EditorGUILayout.EndHorizontal();
        }

        if (GUILayout.Button("添加依赖分组"))
        {
            group.pDependGroups = arrAdd(datas, 0);
        }
    }

    /// <summary>
    /// 从数组中移除某个单项 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="arrary"></param>
    /// <param name="item">  </param>
    private T[] arrRemove<T>(T[] arrary, T item)
    {
        var ret = new T[arrary.Length - 1];
        for (int i = 0, j = 0; i < ret.Length; i++, j++)
        {
            var old = arrary[j];
            if (old.Equals(item))
                old = arrary[++j];
            ret[i] = old;
        }
        return ret;
    }

    /// <summary>
    /// 从数组中移除某个单项 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="arrary"></param>
    /// <param name="item">  </param>
    private T[] arrAdd<T>(T[] arrary, T item)
    {
        var ret = new T[arrary.Length + 1];
        arrary.CopyTo(ret, 0);
        ret[arrary.Length] = item;
        return ret;
    }
}